package com.zb.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjStoreSet;
import com.zb.enums.SystemSetEnum;
import com.zb.mapper.ZjStoreSetMapper;
import com.zb.service.StoreTypeDefaultDataService;
import com.zb.service.ZjStoreSetService;
import com.zb.vo.StoreSetVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 仓储设置表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-07-25
 */
@Service
public class ZjStoreSetServiceImpl extends ServiceImpl<ZjStoreSetMapper, ZjStoreSet> implements ZjStoreSetService {
    @Resource
    private StoreTypeDefaultDataService storeTypeDefaultDataService;
    // 初始化 或 恢复初始化仓库设置
    @Override
    public void initOrRestoreDefaultStoreSet(Long custAccId) {
        ZjStoreSet storeSet = new ZjStoreSet();
        storeSet.setId(custAccId);
        storeSet.setOutboundCodePrefix("CK");
        storeSet.setInboundCodePrefix("RK");
        storeSet.setCodeActiveMode(SystemSetEnum.STORE_CODE_ACTIVE_MODE_1.getType());
        this.saveOrUpdate(storeSet);
    }

    // 初始化默认值
    @Override
    public void initDefaultValue(Long custAccId) {
        initOrRestoreDefaultStoreSet(custAccId);
        storeTypeDefaultDataService.save(custAccId);
    }

    // 获取仓库设置
    @Override
    public StoreSetVo getStorerSet(Long custAccId) {
        ZjStoreSet storeSet = this.getById(custAccId);
        return StoreSetVo.beanToVo(storeSet);
    }

    // 保存仓库设置
    @Override
    public void saveStoreSet(StoreSetVo storeSetVo) {
        ZjStoreSet storeSet = StoreSetVo.voToBean(storeSetVo);
        this.updateById(storeSet);
    }

    // 根据商家ID获取出库前缀
    @Override
    public String getOutStorePrefix(Long custAccId) {
        StoreSetVo setVo = getStorerSet(custAccId);
        return null == setVo? "CK" : setVo.getOutboundCodePrefix();
    }

    // 根据商家ID获取入库前缀
    @Override
    public String getInStorePrefix(Long custAccId) {
        StoreSetVo setVo = getStorerSet(custAccId);
        return null == setVo? "RK" : setVo.getInboundCodePrefix();
    }

    // 根据商家ID获取出库编码的激活方式
    @Override
    public Integer getOutStoreCodeActiveMode(Long custAccId) {
        StoreSetVo setVo = getStorerSet(custAccId);
        return null == setVo? SystemSetEnum.STORE_CODE_ACTIVE_MODE_1.getType() : setVo.getCodeActiveMode();
    }

}
